//
//  NMPOLaunchView.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/11/1.
//

import UIKit

class NMPOLaunchView: UIView {

    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        addSubview(nmpo_bgView)
       
        addSubview(nmpo_titleView)
        addSubview(nmpo_progressBgView)
        nmpo_progressBgView.addSubview(nmpo_progressView)
        nmpo_bgView.snp.makeConstraints { make in
            make.bottom.left.width.equalToSuperview()
            make.top.equalToSuperview().offset(-50)
        }
        nmpo_progressBgView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 92, height: 4))
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-54)
        }
        nmpo_titleView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 134, height: 24))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(nmpo_progressBgView.snp.top).offset(-25)
        }

    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - nmpo action
    
    func nmpo_hiddenView() {
        nmpo_hiddenBlcok?()
        UIView.animate(withDuration: 0.25) {
            self.alpha = 0
        } completion: { _ in
            
        }
    }
    
    func nmpo_startLoading() {
        nmpo_value = 0
        nmpo_speed = 10
        isHidden = false
        alpha = 1
        nmpo_timeName = NMPOTimerVender.nmpo_timeTask(self, selector: #selector(nmpo_timeSum), start: 0, interval: 0.01, repeats: true, async: false, name: nmpo_timeName)
    }
    
    func nmpo_stopLoading() {
        NMPOTimerVender.nmpo_cancelTask(nmpo_timeName)
        nmpo_value = 1
        nmpo_hiddenView()
    }
    
    func nmpo_cancelLoading() {
        NMPOTimerVender.nmpo_cancelTask(nmpo_timeName)
        nmpo_value = 0
        isHidden = true
    }
    
    @objc func nmpo_timeSum()  {
        if nmpo_value >= 92 {
            nmpo_stopLoading()
            return
        }
        nmpo_value += nmpo_step * nmpo_speed
    }
    
    
    //MARK: - nmpo po
    var nmpo_value: CGFloat = 0 {
        didSet {
            nmpo_progressView.frame = CGRect(x: 0, y: 0, width: nmpo_value, height: 4)
        }
    }
    var nmpo_speed: CGFloat = 1
    var nmpo_timeName: String = "nmpo_timeName"
    var nmpo_hiddenBlcok:(()->())?
    var nmpo_isShowAd = false
    var nmpo_step:CGFloat = 92 / CGFloat(15) * 0.01
    //MARK: - nmpo lazy
    lazy var nmpo_bgView: UIImageView = {
        let nmpo_bgView = UIImageView(image: UIImage(named: "nmpo_launch_bg"))
        nmpo_bgView.contentMode = .scaleAspectFill
        return nmpo_bgView
    }()
   
    lazy var nmpo_titleView: UIImageView = {
        let nmpo_titleView = UIImageView(image: UIImage(named: "nmpo_launch_title"))
        return nmpo_titleView
    }()
    lazy var nmpo_progressBgView: UIView = {
        let nmpo_progressBgView = UIView()
        nmpo_progressBgView.backgroundColor = NMPOCommon.nmpo_getHXColorValue(rgb: 0x3E1B0A)
        return nmpo_progressBgView
    }()
    lazy var nmpo_progressView: UIImageView = {
        let nmpo_progressView = UIImageView(image: UIImage(named: "nmpo_launch_progress"))
        return nmpo_progressView
    }()
}

